<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['shortPlay:shortPlayInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['shortPlay:shortPlayInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['shortPlay:shortPlayInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['shortPlay:shortPlayInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shortPlayInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="封面" align="center">
        <template #default="scope">
          <ImagePreview :src="scope.row.icon" width="100px" height="100px"></ImagePreview>
        </template>
      </el-table-column>
      <el-table-column label="剧情简介" align="center" prop="profile" />
      <el-table-column label="热度" align="center" prop="hotNum" />
      <el-table-column label="分类" align="center" prop="typeInfo.name" />
      <el-table-column label="启用状态" align="center" key="state">
        <template #default="scope">
          <el-switch v-model="scope.row.state" :active-value="1" :inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="更新状态" align="center" prop="status">
        <template #default="scope">
          {{ scope.row.status ? '已完结' : '连载中' }}
        </template>
      </el-table-column>
      <el-table-column label="是否精选" align="center" prop="delicate">
        <template #default="scope">
          {{ scope.row.delicate ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['shortPlay:shortPlayInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['shortPlay:shortPlayInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改短剧信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="shortPlayInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="分类" prop="type">
          <el-select v-model="form.type" placeholder="请选择分类">
            <el-option v-for="dict in typeList" :key="dict.id" :label="dict.name" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="封面" prop="icon">
          <ImageUpload v-model="form.icon" :limit="1"></ImageUpload>
        </el-form-item>
        <el-form-item label="全集价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入全集价格" />
        </el-form-item>
        <el-form-item label="剧情简介" prop="profile">
          <el-input v-model="form.profile" placeholder="请输入剧情简介" />
        </el-form-item>
        <el-form-item label="总集数" prop="totalMuster">
          <el-input v-model="form.totalMuster" placeholder="请输入总集数" />
        </el-form-item>
        <el-form-item label="更新状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">已完结</el-radio>
            <el-radio :label="0">连载中</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="热度" prop="hotNum">
          <el-input v-model="form.hotNum" placeholder="请输入热度" />
        </el-form-item>
        <el-form-item label="是否精选" prop="delicate">
          <el-radio-group v-model="form.delicate">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="orderBy">
          <el-input v-model="form.orderBy" placeholder="请输入排序" />
        </el-form-item>
        <el-divider content-position="center">短剧剧集信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddShortPlayMuster">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteShortPlayMuster">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="shortPlayMusterList" :row-class-name="rowShortPlayMusterIndex"
          @selection-change="handleShortPlayMusterSelectionChange" ref="shortPlayMuster">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="资源路径" prop="url" width="150">
            <template #default="scope">
              <FileUpload v-model="scope.row.url" :limit="1" :fileSize="500" :fileType="['mp4']"></FileUpload>
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="price" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.price" placeholder="请输入价格" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="orderBy" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.orderBy" placeholder="请输入排序" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ShortPlayInfo">
import { listShortPlayInfo, getShortPlayInfo, delShortPlayInfo, addShortPlayInfo, updateShortPlayInfo, editState } from "@/api/shortPlay/shortPlayInfo";

import { childrenByCode } from "@/api/system/type"

const { proxy } = getCurrentInstance();

const shortPlayInfoList = ref([]);
const shortPlayMusterList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedShortPlayMuster = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 列显隐信息
const columns = ref([
  { key: 0, label: `用户编号`, visible: true },
  { key: 1, label: `用户名称`, visible: true },
  { key: 2, label: `用户昵称`, visible: true },
  { key: 3, label: `区域`, visible: true },
  { key: 4, label: `手机号码`, visible: true },
  { key: 5, label: `状态`, visible: true },
  { key: 6, label: `创建时间`, visible: true },
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    type: null,
    state: null,
    status: null,
    delicate: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/**
 * 获取视频分类
 */

const typeList = ref([]);
const getTypeList = () => {
  childrenByCode({ code: "duanju_type" }).then(response => {
    typeList.value = response.data;
  });
}
getTypeList();

function handleStatusChange(row) {
  let text = row.state == 1 ? "启用" : "停用";
  proxy.$modal
    .confirm('确认要"' + text + '""' + row.name + '"吗?')
    .then(function () {
      return editState({
        id: row.id,
        state: row.state
      });
    })
    .then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(function () {
      row.state = row.state == 0 ? 1 : 0;
    });
}


/** 查询短剧信息列表 */
function getList() {
  loading.value = true;
  listShortPlayInfo(queryParams.value).then(response => {
    shortPlayInfoList.value = response.rows;
    total.value = response.total;
    setTimeout(() => {
      loading.value = false;
    }, 100);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    icon: null,
    profile: null,
    hotNum: null,
    type: null,
    state: null,
    status: null,
    delicate: 0,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    isDelete: null,
    orderBy: null,
    totalMuster: null,
    price: 0
  };
  shortPlayMusterList.value = [];
  proxy.resetForm("shortPlayInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加短剧信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getShortPlayInfo(_id).then(response => {
    form.value = response.data;
    shortPlayMusterList.value = response.data.shortPlayMusterList;
    open.value = true;
    title.value = "修改短剧信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["shortPlayInfoRef"].validate(valid => {
    if (valid) {
      form.value.shortPlayMusterList = shortPlayMusterList.value;
      if (form.value.id != null) {
        updateShortPlayInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addShortPlayInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除短剧信息编号为"' + _ids + '"的数据项？').then(function () {
    return delShortPlayInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 短剧剧集序号 */
function rowShortPlayMusterIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 短剧剧集添加按钮操作 */
function handleAddShortPlayMuster() {
  let obj = {};
  obj.url = "";
  obj.orderBy = "";
  obj.isDelete = "";
  obj.price = 0;
  shortPlayMusterList.value.push(obj);
}

/** 短剧剧集删除按钮操作 */
function handleDeleteShortPlayMuster() {
  if (checkedShortPlayMuster.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的短剧剧集数据");
  } else {
    const shortPlayMusters = shortPlayMusterList.value;
    const checkedShortPlayMusters = checkedShortPlayMuster.value;
    shortPlayMusterList.value = shortPlayMusters.filter(function (item) {
      return checkedShortPlayMusters.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleShortPlayMusterSelectionChange(selection) {
  checkedShortPlayMuster.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('shortPlay/shortPlayInfo/export', {
    ...queryParams.value
  }, `shortPlayInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
